<template>
    <n-form
        ref="formRef"
        label-placement="top"
        :label-width="80"
        :model="form"
        :rules="rules"
    >

        <n-form-item label="名称" path="name">
            <jb-input v-model:value="form.name" placeholder="请输入名称" />
        </n-form-item>
        <n-form-item label="电话" path="phone">
            <jb-input v-model:value="form.phone" placeholder="请输入电话" />
        </n-form-item>
        <n-form-item label="地址" path="address">
            <jb-input v-model:value="form.address" placeholder="请输入地址" />
        </n-form-item>

    </n-form>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import type { FormInst, FormRules } from 'naive-ui'
import { Rules } from '@/utils'
import { JBoltApi } from '@/utils/request'
import { useResetableData } from '@/hooks/common/use-resetable-data'
import { ResData } from '@/typings/request'

const props = withDefaults(
    defineProps<{
        id?: string
    }>(),
    {
        id: ''
    }
)

/** 表单相关 start */
const formRef = ref<FormInst | null>()
interface PostType {
    id: string
    name: string
    address: string
    phone: string
    is_deleted:number
}
const rules: FormRules = {
    // name: new Rules().required('请输入姓名').value,
    // userName: new Rules().required('请输入用户名').value,
    // type: new Rules().required('请选择类型').value,
    // sn: new Rules().required('请输入编码').value,
    // age: new Rules().gt(0).lt(150).value,
    // phone: new Rules().required().phone().value,
    // email: new Rules().email().value,
    // password: new Rules().minLength(6, '密码不能少于6位').value
}

const form = useResetableData<PostType>({
    id: '',
    name: '',
    address:'',
    phone:'',
    is_deleted:0
})

/**
 * 提交表单
 */
async function submit() {
    await formRef.value?.validate()
    let url = props.id ? '/api/admin/xdScmSupplier/update' : '/api/admin/xdScmSupplier/save'

    await JBoltApi.tryPost(url, form)
    return true
}

function loadEditData() {
    JBoltApi.get<ResData>(`/api/admin/xdScmSupplier/${props.id}`).then(
        ({ error, result }) => {
            if (error) return
            form._reset(result.data)
        }
    )
}

onMounted(() => {
    if (props.id) {
        loadEditData()
    }
})

defineExpose({
    submit
})

/** 表单相关 end */
</script>
<style scoped></style>
